﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class System_Purview : ATPages
{
    ZSql menu = new ZSql();
    ZSql info = new ZSql();
    ZSql qx = new ZSql();

    static string nopermission = ",0,";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["t"] == "2")
                lblHint.Text = " 蓝色表示用户所属角色所拥有的菜单权限";

            hidId.Value = Request["id"].ToString();

            if (Request["pn"] != null)
                hidPageNum.Value = Request["pn"].ToString();

            if (Request["deptid"] != null)
                hidDeptid.Value = Request["deptid"].ToString();
            
            InitTree();
            
            InitData();
        }
    }


    private void InitData()
    { 
        string name;

        DataTable roleinfo = new DataTable();
        DataTable purview = new DataTable();

        if (Request["t"] == "1")
        {
            //角色权限
            name = info.GetScalar("select role from role where roleid = " + hidId.Value).ToString();

            qx.Open("select permission from purview where roleid = " + hidId.Value);

            purview = qx.m_table;

            qx.Close();
        }
        else
        {
            //用户权限及其角色权限
            name = info.GetScalar("select username from userinfo where userid = " + hidId.Value).ToString();

            string roleid = info.GetScalar("select roleid from userinfo where userid =" + hidId.Value).ToString();

            qx.Open("select permission from purview where roleid = " + roleid);

            roleinfo = qx.m_table;

            qx.Close();

            info.Open("select permission from purview where userid = " + hidId.Value);

            purview = info.m_table;

            info.Close();
        }

        lblTitle.Text = name + " 权限设置";

        string rolepermission = ",";
        string permission = ",";

        if (purview.Rows.Count == 0)
        {
            permission = nopermission;
        }
        else
        {
            foreach (DataRow row in purview.Rows)
            {
                permission += row[0].ToString() + ",";
            }
        }

        if (roleinfo.Rows.Count == 0)
        {
            rolepermission = nopermission;
        }
        else
        {
            foreach (DataRow row in roleinfo.Rows)
            {
                rolepermission += row[0].ToString() + ",";
            }
        }

        InitPurView(tvPurview.Nodes[0], permission, rolepermission);

        if (tvPurview.CheckedNodes.Count > 0)
            tvPurview.Nodes[0].Checked = true;
    }


    private void InitTree()
    {
        menu.Open("select menuid,menu,parentid from menu");
        DataTable dt = menu.m_table;
        menu.Close();

        TreeViewClass.InitTree(tvPurview, null, new DataTable[] { dt }, new string[] { "parentid" }, new string[] { "menuid" }, new string[] { "menu" }, null, null, new bool[] { false }, 0, "菜单权限列表",null,null);

        tvPurview.ShowCheckBoxes = TreeNodeTypes.All;
        tvPurview.ShowLines = true;
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        StringBuilder data = new StringBuilder();

        if (tvPurview.CheckedNodes.Count == 0)
        {
            if (Request["t"] == "1")
            {
                qx.Open("delete from purview where roleid = " + hidId.Value);
            }
            else
            {
                qx.Open("delete from purview where userid = " + hidId.Value);
            }
        }
        else
        {
            foreach (TreeNode node in tvPurview.CheckedNodes)
            {
                if (node.Depth > 0)
                    data.Append(node.Value + ",");
            }

            string purview = data.Remove(data.Length - 1, 1).ToString();

            if (Request["t"] == "1")
            {
                qx.Open("select * from purview where roleid = " + hidId.Value);
                if (!qx.NextRow())
                    qx.NewRow();

                qx["roleid"] = hidId.Value;
                qx["permission"] = purview;
                qx.Update();
            }
            else
            {
                qx.Open("select * from purview where userid = " + hidId.Value);
                if (!qx.NextRow())
                    qx.NewRow();

                qx["userid"] = hidId.Value;
                qx["permission"] = purview;
                qx.Update();
            }
        }

        qx.Close();

        btnBack_Click(sender, e);
    }

    //初始化权限数据
    private void InitPurView(TreeNode tn,string data,string rolepermission)
    {
        foreach (TreeNode node in tn.ChildNodes)
        {
            if (data.IndexOf("," + node.Value + ",") == -1)
            {
                node.Checked = false;
            }
            else
            {
                node.Checked = true;
            }

            if (rolepermission.IndexOf("," + node.Value + ",") > -1)
            {
                node.Text = "<font color='blue'>" + node.Text + "</font>";
            }

            InitPurView(node, data,rolepermission);
        }
    }


    protected void btnBack_Click(object sender, EventArgs e)
    {
        if (hidPageNum.Value == "")
        {
            Response.Redirect("Role.aspx?moduid="+Request["moduid"]);
        }
        else
        {
            if (hidDeptid.Value == "")
            {
                Response.Redirect("User.aspx?pn=" + hidPageNum.Value);
            }
            else
            {
                Response.Redirect("User.aspx?deptid=" + hidDeptid.Value + "&pn=" + hidPageNum.Value);
            }
        }
    }
}
